﻿using CPPEI.Coolzon.Configuration.Client;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.Designer.Event.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Xml;
using CPPEI.Coolzon.Metadata.ParserV1;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for ToolboxView.xaml
    /// </summary>
    public partial class ToolboxView : View
    {
        public ToolboxView()
        {
            InitializeComponent();

            UIEventBus.Register(this);
            RenderControl();
        }
        private void RenderControl()
        {
            List<CPPEI.Coolzon.Metadata.PartLibItem> partLibItemList = new List<Coolzon.Metadata.PartLibItem>();
            CPPEI.Coolzon.Metadata.PartLibItem pointer = new PartLibItem();
            pointer.Name = "Pointer";
            pointer.Caption = "指针";
            pointer.GroupName = "指针";
            partLibItemList.Add(pointer);

            PartLibsConfig partLibsConfig = Config.PartLibs;

            for (int partLibIndex = 0; partLibIndex < Config.PartLibs.PartLibs.Count; partLibIndex++)
            {
                Configuration.Client.PartLib partlib = Config.PartLibs.PartLibs[partLibIndex];
                string filePath = string.Format("{0}{1}{2}.xml", Config.PartLibs.PartLibsBaseAddress, Config.PartLibs.PartLibsDirectory, partlib.Name);

                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(filePath);

                CPPEI.Coolzon.Metadata.PartLib partLib = PartLibXmlProxy.Load(filePath);
                partLibItemList.AddRange(partLib.PartList);
            }
            toolList.ItemsSource = partLibItemList;
            System.ComponentModel.ICollectionView view = CollectionViewSource.GetDefaultView(toolList.ItemsSource);
            view.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));
        }

        private void toolList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CPPEI.Coolzon.Metadata.PartLibItem item = toolList.SelectedItem as CPPEI.Coolzon.Metadata.PartLibItem;
            if(string.Equals(item.Name, "Pointer"))
            {
                UIEventBus.Publish(new UISelectPartLibItemEvent(null));
            }
            else
            {
                UIEventBus.Publish(new UISelectPartLibItemEvent(item));
            }
        }

        [Common.Subscribe]
        public void OnPartCreatedComplete(UIPartCreatedCompleteOrCancelEvent @event)
        {
            SetSelectPointer();
        }

        private void SetSelectPointer()
        {
            List<PartLibItem> partLibItems = toolList.Items.Cast<PartLibItem>().ToList();
            PartLibItem partLibItem = partLibItems.Where(p => p.Name == "Pointer").First();
            toolList.SelectedItem = partLibItem;
        }

        [Common.Subscribe]
        public void OnPartCreatedComplete(Event.UI.UIToolboxSelectPointerEvent @event)
        {
            SetSelectPointer();
        }

        private void MenuItemList_Click(object sender, RoutedEventArgs e)
        {
            toolList.Style = Resources["ListStyle"] as Style;
        }

        private void MenuItemTile_Click(object sender, RoutedEventArgs e)
        {
            toolList.Style = Resources["TileStyle"] as Style;
        }
    }
}
